class SoundManage {
	//音乐对象，存放背景音乐
	public static yinyueChannel: egret.SoundChannel;
	private static yinyueSound: egret.Sound;
	//声音锁，进入后台时不允许播放声音
	public static lock = true;

	//进入前台需要点击屏幕才解锁声音
	public static UnlockAudio() {
		egret.MainContext.instance.stage.removeEventListener(egret.TouchEvent.TOUCH_TAP, SoundManage.UnlockAudio, this);
		SoundManage.lock = false;
		SoundManage.stopYinYue();
		SoundManage.startYinYueLoad();
	}
	/*************** 音乐 start ************************/
	public static startYinYueLoad(): void {
		if (SoundManage.lock) {
			return;
		}
		var yinYueState = egret.localStorage.getItem("yinYueState");
		if (yinYueState != "false") {
			if (this.yinyueChannel == null && this.yinyueSound == null) {
				if (InstructionsEnum.SOUND_GAME != "") {
					this.yinyueSound = RES.getRes(InstructionsEnum.SOUND_GAME);
					if (this.yinyueSound) {
						SoundManage.onLoadYinYueComplete()
					}
				}
			}
		}
	}
	private static onLoadYinYueComplete(): void {
		this.yinyueChannel = this.yinyueSound.play(0, 0);
		var yinYuevolumeValue = egret.localStorage.getItem("yinYuevolumeValue");
		if (yinYuevolumeValue != null) {
			this.yinyueChannel.volume = Number(yinYuevolumeValue);
		}
	}
	public static stopYinYue(): void {
		if (this.yinyueChannel != null) {
			this.yinyueChannel.stop();
			this.yinyueChannel = null;
			this.yinyueSound = null;
		}
	}
	/*************** 音乐 end ************************/

	/************************ 音效 start ************************/
	public static startYinxiaoLoad(name: string, loop: number = 1): void {
		if (SoundManage.lock) {
			return;
		}
		var yinXiaoState = egret.localStorage.getItem("yinXiaoState");
		if (yinXiaoState != "false") {
			var sound: egret.Sound = RES.getRes(name);
			if (sound) {
				var yinXiaovolumeValue = egret.localStorage.getItem("yinXiaovolumeValue");
				var Channel = sound.play(0, loop);
				Channel.addEventListener(egret.Event.SOUND_COMPLETE, () => {
					Channel.stop();
					Channel = null;
					sound = null;
				}, this);
				if (yinXiaovolumeValue != null) {
					Channel.volume = Number(yinXiaovolumeValue);
				}
			} else {
				egret.log("读取不到音频文件：" + name)
			}
		}
	}
	/************************** 音效 end ************************/

	//初始化声音,程序第一次启动时
	public static changYinLiang() {
		var yinYueState = egret.localStorage.getItem("yinYueState");
		if (yinYueState == null) {
			egret.localStorage.setItem("yinYueState", "true");
		}
		var yinXiaoState = egret.localStorage.getItem("yinXiaoState");
		if (yinXiaoState == null) {
			egret.localStorage.setItem("yinXiaoState", "true");
		}
		var yinYuevolumeValue = egret.localStorage.getItem("yinYuevolumeValue");
		if (yinYuevolumeValue == null) {
			egret.localStorage.setItem("yinYuevolumeValue", "1");
		}
		var yinXiaovolumeValue = egret.localStorage.getItem("yinXiaovolumeValue");
		if (yinXiaovolumeValue == null) {
			egret.localStorage.setItem("yinXiaovolumeValue", "1");
		}
	}

	public static startAnniuBtnLoad() {
		SoundManage.startYinxiaoLoad("Button_mp3", 1);
	}
}